package com.crm.service.saling.impl;

import com.crm.dao.saling.BackDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.Back;
import com.crm.service.saling.BackService;
import com.crm.vo.BackClientContractUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BackServiceImpl implements BackService {
    @Autowired
    private BackDao backDao;

    @Override
    public List<Back> getBackRecordByContractId(Long contractId) throws ServiceException {
        try {
            return backDao.getBackRecordByContractId(new BigDecimal(contractId));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 获取一个回款记录
     *
     * @param backId
     * @return
     * @throws ServiceException
     */
    @Override
    public Back getBackByBackId(Long backId) throws ServiceException {
        try {
            return backDao.getBackByBackId(backId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 获取回款记录
     *
     * @param page
     * @param rows
     * @return
     * @throws ServiceException
     */
    @Override
    public List<BackClientContractUserInfoVO> getBackClientContractUserInfoVOList(int page, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(page - 1, rows);
            return backDao.getBackClientContractUserInfoVOList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 获取回款记录数量
     *
     * @return
     */
    @Override
    public Long getBackCount() {
        return backDao.getBackCount();
    }

    /**
     * 存储一个回款记录
     *
     * @param back
     * @return
     * @throws ServiceException
     */

    @Override
    public Back saveBackRecord(Back back) throws ServiceException {
        try {
            return backDao.save(back);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }
}
